import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Question5 {

    static int ret = 0;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        int n = in.nextInt();
        
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = in.nextInt();
        }

        boolean[] isEdge = new boolean[n - 1]; 
        int[][] edges = new int[n - 1][2]; 
        for (int i = 0; i < n - 1; i++) {
            edges[i][0] = in.nextInt();
            edges[i][1] = in.nextInt();

            isEdge[i] = isNum(values[edges[i][0] - 1], values[edges[i][1] - 1]);
        }

        int[] visNode = new int[n];
        int[] visEdge = new int[n - 1];
        int tol = 0;
        
        // 遍历每条边
        for (int i = 0; i < n - 1; i++) {
            if (isEdge[i]) {
                // System.out.println("开始" + i);

                int parent = edges[i][0] - 1;
                int child = edges[i][1] - 1;

                visEdge[i] = 1;
                visNode[parent] = 1;
                visNode[child] = 1;
                dfs(edges, i, visNode, visEdge, tol, isEdge);
                visNode[parent] = 0;
                visNode[child] = 0;
                visEdge[i] = 0;

                // Arrays.fill(visNode, 0);

                // System.out.println("结束" + ret);
            }

            // System.out.println(ret);
        }

        System.out.println(ret);
    }

    public static void dfs(int[][] edges, int index, int[] visNode, int[] visEdge, int tol, boolean[] isEdge) {

        tol += 2;
        ret = Math.max(ret, tol);

        for (int i = 0; i < edges.length; i++) {
            int parent = edges[i][0] - 1;
            int child = edges[i][1] - 1;

            if (visEdge[i] == 1 || !isEdge[i] || (visNode[parent] + visNode[child] > 0)){
                continue ;
            }

            visEdge[i] = 1;
            visNode[parent] = 1;
            visNode[child] = 1;
            dfs(edges, index, visNode, visEdge, tol, isEdge);
            visNode[parent] = 0;
            visNode[child] = 0;
            visEdge[i] = 0;
        }
    }

    // 判断是否为完全平方数
    public static boolean isNum (int x, int y) {
        long mul = x * y;

        long xx =  (long)Math.sqrt(mul);

        return xx * xx == mul; 
    }
}